var express = require('express');
var router = express.Router();

const { login2 } = require('../server/usersserver');
//登录验证
router.post('/Login', async function (req, res, next) {
  const loginData = req.body;
  const data = await login2(loginData);
  if (data.length > 0) {
    console.log(data[0]);
    
    if (data[0].admin_status) {
      req.session.regenerate(function () {
        req.session.loginUser = data[0].admin_name;//登录成功保存用户在后端
        console.log(req.session.loginUser);
        res.send(true)
      })
    } else {
      res.send(false) //当前用户存在，但禁用状态返回false
    }
  } else {
    res.send(false) //用户不存在
  }

});
//保持登录状态
router.get('/islogin', async function (req, res) { 
  const sure = req.session.loginUser;
  if (sure) {
    res.send({ islogin: true,
    name:sure })
  } else {
    res.send({ islogin: false })
  }
})
//退出登录状态
router.get('/logout',async function(req,res){
  req.session.destroy(function(){
   // req.session.loginUser = null;
    res.clearCookie('currentUser');
    res.send(true)
  })
 
})
module.exports = router;
